import re


class ValidatorUtils:
    """
        校验工具类,使用正则表达式进行校验
    """

    @staticmethod
    def validate_by_pattern(pattern, value):
        """
            根据正则表达式校验
        """
        return re.match(pattern, value) is not None

    @staticmethod
    def validate_email(email):
        """
            校验邮箱
        """
        pattern = r'^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$'
        return re.match(pattern, email) is not None

    @staticmethod
    def validate_phone(phone):
        """
            校验手机号
        """
        pattern = r'^1[3-9]\d{9}$'
        return re.match(pattern, phone) is not None

    @staticmethod
    def validate_idcard(idcard):
        """
            校验身份证号
        """
        pattern = r'^\d{17}[\dXx]$'
        return re.match(pattern, idcard) is not None

    @staticmethod
    def validate_url(url):
        """
            校验URL
        """
        pattern = r'^https?://(?:[-\w.]|(?:%[\da-fA-F]{2}))+$'
        return re.match(pattern, url) is not None

    @staticmethod
    def validate_ip(ip):
        """
            校验IP地址
        """
        pattern = r'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
        return re.match(pattern, ip) is not None

    @staticmethod
    def validate_port(port):
        """
            校验端口号
        """
        pattern = r'^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$'
        return re.match(pattern, port) is not None


